import { Message } from 'element-ui'
import { loginUKey } from '@/api/login'
import vm from '../main.js'

var ctrl = null
var websock = true
var ArmHandle = null // 加密锁句柄
var b = new Base64()
var userInfo = null
var Index = 0
var goMenu = null
var colseLoading = null

export function OnLoad (colseLoading) {
  return new Promise((resolve, reject) => {
    try {
      ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
      resolve(ctrl)
    } catch (e) {
      ctrl = null
      websock = false
      resolve()
    }
  })
}

// 查找RockeyArm
export function Arm_Enum (data) {
  userInfo = data[0]
  goMenu = data[1]
  colseLoading = data[2]
  if (!websock) {
    try {
      ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
      ctrl.Arm_Enum()
    } catch (e) {
      Message.error('验证失败')
    }
  } else {
    try {
      ctrl.Arm_Enum(function (result, response) {
        if (!result) {
          // alert("Arm_Enum error. " + response);
          colseLoading()
          Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        } else {
          // var rtn = response
          Arm_Open()
        }
      })
    } catch (e) {
      ctrl = null
      websock = false
      Message.error(vm.$t('验证失败'))
      colseLoading()
    }
  }
}

// 打开RockeyArm
export function Arm_Open () {
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    ArmHandle = ctrl.Arm_Open(parseInt(Index))
  } else {
    ctrl.Arm_Open(function (result, response) {
      if (!result) {
        // alert("Arm_Open error. " + response);
        colseLoading()
        Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        ArmHandle = response
        Arm_VerifyPIN()
      }
    }, Index)
  }
}
// 验证PIN码
export function Arm_VerifyPIN () {
  var Handle = ArmHandle
  var UserPin = 'likonghuakang123'
  var UserType = 1
  // 获取要验证的用户类型
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_VerifyPIN(parseInt(Handle), UserType, UserPin)
    if (rtn === '0') {
      Message.error(vm.$t('验证PIN码成功'))
      // alert("<spring:message code='验证PIN码成功'></spring:message>")
    } else {
      alert(rtn)
    }
  } else {
    ctrl.Arm_VerifyPIN(function (result, response) {
      if (!result) {
        // alert("Arm_VerifyPIN error. " + response);
        colseLoading()
        Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        if (rtn === '0') {
          Arm_ReadFile()
        } else {
          // alert(rtn);
          colseLoading()
          Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        }
      }
    }, parseInt(Handle), UserPin, UserType)
  }
}

// 读文件
export function Arm_ReadFile () {
  var Handle = ArmHandle
  var FileID = 1
  var ReadLength = 1024
  var FileOffset = 0
  var FileType = 1

  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_ReadFile(parseInt(Handle), parseInt(FileID), parseInt(FileOffset), parseInt(ReadLength))
  } else {
    ctrl.Arm_ReadFile(function (result, response) {
      if (!result) {
        // alert("Arm_ReadFile error. " + response);
        colseLoading()
        Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        var DataInput = b.decode(rtn.split('AAAAAAAAAA')[0].replace(/\s+/g, '')).match(/[A-Za-z0-9+/=]+/g)[0]
        var form = {
          ukey: DataInput,
          username: userInfo.username,
          role: userInfo.role,
          password_past: userInfo.password_past,
          session_id: userInfo.session_id
        }
        loginUKey(form).then(res => {
          goMenu(userInfo, res.status)
          colseLoading()
          Arm_Close()
        }).catch(e => {
          colseLoading()
          Arm_Close()
        })
        // $.ajax({
        //   type: 'POST',
        //   url: `${ctx}/loginUkey`,
        //   data: {
        //     ukey: DataInput,
        //     username: username,
        //     role: role,
        //     password_past: password_past
        //   },
        //   dataType: 'json',
        //   success: function (result) {
        //     if (result.success) {
        //       $('#submit').attr('disabled', false)
        //       window.location.href = '${ctx}/index'
        //       Arm_Close()
        //     } else {
        //       Arm_Close()
        //       $('#submit').attr('disabled', false)
        //       // alert(result.msg);
        //       Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
        //       // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        //     }
        //   }
        // })
      }
    }, parseInt(Handle), FileID, FileOffset, ReadLength)
  }
}

// 关闭RockeyArm
export function Arm_Close () {
  var Handle = ArmHandle
  if (!websock) {
    ctrl = new AtlCtrlForRockeyArm('{33020048-3E6B-40BE-A1D4-35577F57BF14}')
    var rtn = ctrl.Arm_Close(parseInt(Handle))
    if (rtn === '0') {
      alert("<spring:message code='关闭锁成功'></spring:message>")
    } else {
      // alert(rtn);
      colseLoading()
      Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
      // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
    }
  } else {
    ctrl.Arm_Close(function (result, response) {
      if (!result) {
        // alert("Arm_Close error. " + response);
        colseLoading()
        Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
        // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
      } else {
        var rtn = response
        if (rtn === '0') {
          colseLoading()
        } else {
          // alert(rtn);
          colseLoading()
          Message.error(vm.$t('Ukey验证失败, 请插入正确Ukey, 再次验证'))
          // alert("<spring:message code='Ukey验证失败, 请插入正确Ukey, 再次验证'></spring:message>!")
        }
      }
    }, Handle)
  }
}

export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function (Vue) {
    Vue.prototype.OnLoadRead = (param) => OnLoad(param)
    Vue.prototype.Arm_Enum_Read = (param) => Arm_Enum(param)
  }
}
